package edu.stthomas.edu.seis770.stocksystem;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;

public class StockSimulator {
    private static StockMonitor stockMonitor;
    private static StockBroker stockBroker;
	private JFrame frmStockSimulator;
	private JTextField stockSymbol;
	private JTextField stockPrice;
	private JLabel lblStockSymbol;
	private JLabel lblStockPrice;
	private JButton btnUpdateStock;
	private JTextArea outputTextArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StockSimulator window = new StockSimulator();
					window.frmStockSimulator.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public StockSimulator() {
        stockBroker = new StockBroker();
        stockMonitor = new StockMonitor();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmStockSimulator = new JFrame();
		frmStockSimulator.setTitle("Stock Simulator");
		frmStockSimulator.setBounds(100, 100, 758, 468);
		frmStockSimulator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel newStock = new JPanel();
		
		outputTextArea = new JTextArea();
		GroupLayout groupLayout = new GroupLayout(frmStockSimulator.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(outputTextArea, GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
						.addComponent(newStock, GroupLayout.PREFERRED_SIZE, 463, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(newStock, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(outputTextArea, GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
					.addContainerGap())
		);
		//Show System.Output to Text Area
		redirectSystemStreams();
		newStock.setLayout(null);
		
		
		
		stockSymbol = new JTextField();
		stockSymbol.setBounds(104, 13, 80, 22);
		newStock.add(stockSymbol);
		stockSymbol.setColumns(10);
		
		stockPrice = new JTextField();
		stockPrice.setBounds(104, 48, 80, 22);
		newStock.add(stockPrice);
		stockPrice.setColumns(10);
		
		JButton btnCreateStock = new JButton("Create Stock");
		btnCreateStock.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				outputTextArea.setText("");
				String symbol = stockSymbol.getText();
				Money price = new Money(Double.parseDouble(stockPrice.getText()));
				Stock s = new Stock(symbol, new StockStatus(price));
			}
		});
		btnCreateStock.setBounds(212, 47, 116, 25);
		newStock.add(btnCreateStock);
		
		lblStockSymbol = new JLabel("Stock Symbol");
		lblStockSymbol.setBounds(12, 16, 92, 16);
		newStock.add(lblStockSymbol);
		
		lblStockPrice = new JLabel("Stock Price");
		lblStockPrice.setBounds(12, 51, 80, 16);
		newStock.add(lblStockPrice);
		
		btnUpdateStock = new JButton("Update Stock");
		btnUpdateStock.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				outputTextArea.setText("");
				Stock s = stockBroker.getStock(stockSymbol.getText());
				Money price = new Money(Double.parseDouble(stockPrice.getText()));
				s.addStatus(new StockStatus (price));
			}
		});
		btnUpdateStock.setBounds(340, 47, 111, 25);
		newStock.add(btnUpdateStock);
		frmStockSimulator.getContentPane().setLayout(groupLayout);
	}
	
	// Redirect System.output
	 private void updateTextArea(final String text) {
		    SwingUtilities.invokeLater(new Runnable() {
		      public void run() {
		        outputTextArea.append(text);
		      }
		    });
		  }

		  private void redirectSystemStreams() {
		    OutputStream out = new OutputStream() {
		      @Override
		      public void write(int b) throws IOException {
		        updateTextArea(String.valueOf((char) b));
		      }

		      @Override
		      public void write(byte[] b, int off, int len) throws IOException {
		        updateTextArea(new String(b, off, len));
		      }

		      @Override
		      public void write(byte[] b) throws IOException {
		        write(b, 0, b.length);
		      }
		    };

		    System.setOut(new PrintStream(out, true));

		  }
}
